<template>
	<view class="ljapp articleList">
		<view class="search-box">
			<u-search
				:showAction="false"
				shape="square"
				placeholder="搜索文章"
				placeholderColor="#999999"
				bgColor="#f6f6f6"
				@search="Onsech"
				>
			</u-search>
		</view>
		
		<view class="content">
			<view class="card" v-for="(item,index) in group.list" :key="index">
				<view class="article-list">
					<view class="item u-flex" @click="gotoDetails(item)">
						<view class="u-flex-fill">
							<view class="tit">{{item.title}}</view>
							<view class="sub-tit">{{item.doctor.name}} | {{item.doctor.practice_organization}}知名医生</view>
							<view class="u-flex u-flex-between">
								<view class="views">{{item.volume}}阅读</view>
								<view class="time">{{$u.timeFormat(item.createtime)}}</view>
							</view>
						</view>
						<image class="cover" :src="$base.cdn(item.img)" mode="aspectFill"></image>
					</view>
					<view class="action-box u-flex u-flex-between">
						<view class="del u-flex u-flex-y-center" @click="onDel(item,index)">
							<image src="/pagesD/static/group_790.png" mode="aspectFill"></image>
							<view>删除</view>
						</view>
						<view class="edit u-flex u-flex-y-center" @click="onEdit(item,index)">
							<image src="/pagesD/static/group_791.png" mode="aspectFill"></image>
							<view>编辑</view>
						</view>
						<block v-if="item.auditstatus==1">
							<view class="down u-flex u-flex-y-center" @click="onDown(item,index)" v-if="item.status=='normal'">
								<image src="/pagesD/static/group_792.png" mode="aspectFill"></image>
								<view>下架</view>
							</view>
							<view class="up u-flex u-flex-y-center" @click="onUp(item,index)" v-else>
								<image src="/pagesD/static/group_793.png" mode="aspectFill"></image>
								<view>上架</view>
							</view>
						</block>
						<view v-else-if="item.auditstatus==2" class="text-danger">未通过</view>
						<view v-else class="text-warning">待审核</view>
					</view>
				</view>
			</view>
			
			<lj-empty v-if="group.loaded&&group.list.length<=0">暂无文章</lj-empty>
			<view v-else>
				<u-loadmore
					:status="loading?'loading':(group.loaded?'nomore':'loadmore')"
					fontSize="28rpx"
					color="#d9d9d9"
					nomoreText="已加载全部"
					marginTop="30rpx"
					marginBottom="0"
					@loadmore="getList"
				></u-loadmore>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sech: '',
				
				group:{
					page: 1,
					limit: 10,
					list: [],
					loaded: false
				},
				loading: false,
			}
		},
		onLoad() {},
		onShow(){
			this.init()
		},
		methods: {
			init(){
				this.group.page = 1
				this.group.list = []
				this.group.loaded = false
				this.loading = false
			
				this.getList();
			},
			getList(){
				var that = this,group = that.group;
				if(that.loading || group.loaded) return;
			
				that.loading = true
			
				this.$Http({
					url: 'doctorArticleList',
					data: {
						page: group.page,
						limit: group.limit,
						keyword: this.sech,
						isme: 1,
						ismag: 1,
					},
					type:'POST',
				}).then(res=>{
					if(res.code == 1){
						var list = res.data;
						group.loaded = list.length < group.limit;
						group.page ++;
						group.list = group.list.concat(list);
					}
					
					that.loading = false
				}).catch(()=>{
					that.loading = false
				});
			},
			onReachBottom(){
				this.getList()
			},
			gotoDetails(item){
				uni.navigateTo({
					url: '/pagesD/doctor/myArticle?id='+item.id
				})
			},
			Onsech(e){
				this.sech = e
				
				this.init()
			},
			
			onDel(item,index){
				var that = this
				uni.showModal({
					title:"确认删除文章吗？",
					success: (e) => {
						if(e.confirm){
							that.$Http({
								url: 'doctorArticleDel',
								data: {
									id: item.id
								},
								show: true,
								type:'POST',
							}).then(res=>{
								if(res.code == 1){
									that.$u.toast(res.msg)
									that.group.list.splice(index, 1)
								}
							})
						}
					}
				})
			},
			onEdit(item,index){
				uni.navigateTo({
					url: '/pagesD/doctor/articlePublish?id='+item.id
				})
			},
			onDown(item,index){
				var that = this
				uni.showModal({
					title:"确认下架文章吗？",
					success: (e) => {
						if(e.confirm){
							that.$Http({
								url: 'doctorArticleDown',
								data: {
									id: item.id
								},
								show: true,
								type:'POST',
							}).then(res=>{
								if(res.code == 1){
									that.$u.toast(res.msg)
									that.group.list[index].status = 'hidden'
								}
							})
						}
					}
				})
			},
			onUp(item,index){
				var that = this
				uni.showModal({
					title:"确认上架文章吗？",
					success: (e) => {
						if(e.confirm){
							that.$Http({
								url: 'doctorArticleUp',
								data: {
									id: item.id
								},
								show: true,
								type:'POST',
							}).then(res=>{
								if(res.code == 1){
									that.$u.toast(res.msg)
									that.group.list[index].status = 'normal'
								}
							})
						}
					}
				})
			},
		}
	}
</script>

<style lang="less">
	@import '@/pagesD/style.less';
	
	.articleList{
		padding: 0;
	}
	.content{
		padding: 116rpx 20rpx 20rpx;
	}
	.search-box{
		position: fixed;
		width: 100%;
		padding: 0 36rpx 28rpx;
		background-color: #fff;
	}
	
	.action-box{
		padding: 26rpx 36rpx 10rpx;
		
		image{
			width: 32rpx;
			min-width: 32rpx;
			height: 30rpx;
			margin-right: 12rpx;
		}
		
		.del{
			color: #d96666;
		}
		.edit{
			color: #4879fd;
		}
		.up{
			color: #779bfe;
		}
		.down{
			color: #e48e1f;
		}
	}
</style>
